import Vue from 'vue'
import VueRouter from 'vue-router'
import layout from './modules/layout'
import login from './modules/login'
import register from './modules/register'
import NotFoundPage from './modules/NotFoundPage'

Vue.use(VueRouter)

const routes = [
  ...layout,
  ...login,
  ...register,
  ...NotFoundPage
]

const router = new VueRouter({
  routes
})

// 登录拦截-路由全局守卫
// * to 即将进入的组件
// * from 来自那个组件
// * next  放行/跳转
router.beforeEach((to, from, next) => {
  // 非登录页且没有token
  if (!to.meta.auth && !localStorage.token) {
    next('/login')
  } else {
    next()
  }
})

export default router
